
import requests
import time
import hashlib
import os
from bs4 import BeautifulSoup
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36','cookie': 'PHPSESSID=qe3fgvl1i7h29t04vpmuksfev3; __utmt=1; __utma=91659788.737382376.1504191522.1504191522.1504584088.2; __utmb=91659788.64.10.1504584088; __utmc=91659788; __utmz=91659788.1504191527.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)','host':'www.gifbin.com','referer':'http://www.gifbin.com/tag/funny/','connection':'keep-alive'}
count = 0
base_url = "http://www.gifbin.com/thumbs/"
base_path = r"D:\gif"
if not os.path.exists(base_path):
        os.makedirs(base_path)
page = 1
page = input("how page you want?:" )
current_page = int(page)
def try_untill_get(url,what):
        while 1:
                try:
                        if what == 'html':
                                        the_get = requests.get(url,headers=headers).text
                        else:
                                        the_get = requests.get(url,headers=headers).content
                        break
                except:
                        print("maybe be catched,just let me sleep!")
                        time.sleep( 2 )        
        return the_get
        
while current_page > 0:
        url = base_url+str(current_page)
        html = try_untill_get(url,'html')
        print('page:%d get' % current_page)
        soup = BeautifulSoup(html,"html.parser")
        li_list = soup.find_all('li', class_='tooltips')
        for li in li_list:
                link = "http://www.gifbin.com"+li.find('a')['href']
                html = try_untill_get(link,'html')
                print('link:%s done' % link)
                soup = BeautifulSoup(html,"html.parser")
                gif_src = soup.find('div',id='gif-container').find_all('source')[1]['src']
                gif = try_untill_get(gif_src,'gif')
                mini = str(gif[1000:1111]).encode('utf-8')
                name = str(count) + '_' + hashlib.md5(mini).hexdigest() + '.mp4'
                path = base_path + '\\' + name
                with open(path,'wb+') as f:
                        f.write(gif)
                print('gif:%s done' % name)
                count = count + 1
        current_page = current_page - 1
